package com.semidata.trp.business.utils;

import java.util.List;
import java.util.Set;

import com.semidata.rtd.core.json.JsonArray;
import com.semidata.rtd.core.json.JsonObject;
import com.semidata.trp.business.report.MonitorReportData;
import com.semidata.trp.message.business.SyncReport;

public class ReportUtils {
	
	public static SyncReport.Cost getOrganizationCost(Set<MonitorReportData> baseReports, Set<MonitorReportData> currentReports, Integer id) {
		SyncReport.Cost.Builder cost = SyncReport.Cost.newBuilder();
		double baseIn = 0, baseOut = 0, baseAlly = 0;
		double totalIn = 0, totalOut = 0, totalAlly = 0;
		for (MonitorReportData item : baseReports) {
			if (item.getOrganizationCosts().get(id) != null) {
				Double value = item.getOrganizationCosts().get(id).get(0);
				if (value == null)
					value = 0.0;
				baseIn += value;
				value = item.getOrganizationCosts().get(id).get(1);
				if (value == null)
					value = 0.0;
				baseOut += value;
				value = item.getOrganizationCosts().get(id).get(2);
				if (value == null)
					value = 0.0;
				baseAlly += value;
			}
		}

		for (MonitorReportData item : currentReports) {
			if (item.getOrganizationCosts().get(id) != null) {
				Double value = item.getOrganizationCosts().get(id).get(0);
				if (value == null)
					value = 0.0;
				totalIn += value;
				value = item.getOrganizationCosts().get(id).get(1);
				if (value == null)
					value = 0.0;
				totalOut += value;
				value = item.getOrganizationCosts().get(id).get(2);
				if (value == null)
					value = 0.0;
				totalAlly += value;
			}
		}

		double todayIn = totalIn - baseIn;
		if (todayIn < 0)
			todayIn = 0.0;
		double todayOut = totalOut - baseOut;
		if (todayOut < 0)
			todayOut = 0.0;
		double todayAlly = totalAlly - baseAlly;
		if (todayAlly < 0)
			todayAlly = 0.0;
		
		cost.setId(id);
		cost.setTodayIn(todayIn);
		cost.setTotalIn(totalIn);
		cost.setTodayOut(todayOut);
		cost.setTotalOut(totalOut);
		cost.setTodayAlly(todayAlly);
		cost.setTotalAlly(totalAlly);
		return cost.build();
	}
	public static JsonArray getOrganizationCosts(Set<MonitorReportData> baseReports,
			Set<MonitorReportData> currentReports, List<Integer> ids) {

		JsonArray array = new JsonArray();
		for (int id : ids) {
			SyncReport.Cost cost = ReportUtils.getOrganizationCost(baseReports, currentReports, id);
			JsonObject obj = new JsonObject();
			obj.putNumber("id", id);
			obj.putNumber("total_in", cost.getTotalIn());
			obj.putNumber("today_in", cost.getTodayIn());
			obj.putNumber("total_out", cost.getTotalOut());
			obj.putNumber("today_out", cost.getTodayOut());
			obj.putNumber("total_ally", cost.getTotalAlly());
			obj.putNumber("today_ally", cost.getTodayAlly());
			array.add(obj);
		}
		return array;
	}
	public static SyncReport.Impression getResourceImpression (Set<MonitorReportData> baseReports,
			Set<MonitorReportData> currentReports, Integer id) {
		SyncReport.Impression.Builder impression = SyncReport.Impression.newBuilder();
		long base = 0;
		long total = 0;
		for (MonitorReportData item : baseReports) {
			Long value = item.getResourceImpressions().get(id);
			if (value == null)
				value = 0L;
			base += value;
		}

		for (MonitorReportData item : currentReports) {
			Long value = item.getResourceImpressions().get(id);
			if (value == null)
				value = 0L;
			total += value;
		}

		long today = total - base;
		if (today < 0)
			today = 0L;

		
		impression.setId(id);
		impression.setToday(today);
		impression.setTotal(total);
		return impression.build();
	}
	public static JsonArray getResourceImpressions(
			Set<MonitorReportData> baseReports,
			Set<MonitorReportData> currentReports, List<Integer> ids) {
		JsonArray array = new JsonArray();
		for (int id : ids) {
			SyncReport.Impression impression = ReportUtils.getResourceImpression(baseReports, currentReports, id);
			JsonObject obj = new JsonObject();
			obj.putNumber("id", impression.getId());
			obj.putNumber("total", impression.getTotal());
			obj.putNumber("today", impression.getToday());
			array.add(obj);
		}
		return array;
	}
	
	public static SyncReport.Click getResourceClick(Set<MonitorReportData> baseReports,
			Set<MonitorReportData> currentReports, Integer id) {
		SyncReport.Click.Builder click = SyncReport.Click.newBuilder();
		long base = 0;
		long total = 0;
		for (MonitorReportData item : baseReports) {
			Long value = item.getResourceClicks().get(id);
			if (value == null)
				value = 0L;
			base += value;
		}

		for (MonitorReportData item : currentReports) {
			Long value = item.getResourceClicks().get(id);
			if (value == null)
				value = 0L;
			total += value;
		}

		long today = total - base;
		if (today < 0)
			today = 0L;
		click.setId(id);
		click.setToday(today);
		click.setTotal(total);
		return click.build();
	}

	public static JsonArray getResourceClicks(Set<MonitorReportData> baseReports,
			Set<MonitorReportData> currentReports, List<Integer> ids) {

		JsonArray array = new JsonArray();
		for (int id : ids) {
			
			SyncReport.Click click = ReportUtils.getResourceClick(baseReports, currentReports, id);
			JsonObject obj = new JsonObject();
			obj.putNumber("id", click.getId());
			obj.putNumber("total", click.getTotal());
			obj.putNumber("today", click.getToday());
			array.add(obj);
		}
		return array;
	}
	
	public static SyncReport.Cost getStrategyCost(Set<MonitorReportData> baseReports,
			Set<MonitorReportData> currentReports, Integer id) {
		SyncReport.Cost.Builder cost = SyncReport.Cost.newBuilder();
		double baseIn = 0, baseOut = 0, baseAlly = 0;
		double totalIn = 0, totalOut = 0, totalAlly = 0;
		for (MonitorReportData item : baseReports) {
			if (item.getStrategyCosts().get(id) != null) {
				Double value = item.getStrategyCosts().get(id).get(0);
				if (value == null)
					value = 0.0;
				baseIn += value;
				value = item.getStrategyCosts().get(id).get(1);
				if (value == null)
					value = 0.0;
				baseOut += value;
				value = item.getStrategyCosts().get(id).get(2);
				if (value == null)
					value = 0.0;
				baseAlly += value;
			}
		}

		for (MonitorReportData item : currentReports) {
			if (item.getStrategyCosts().get(id) != null) {
				Double value = item.getStrategyCosts().get(id).get(0);
				if (value == null)
					value = 0.0;
				totalIn += value;
				value = item.getStrategyCosts().get(id).get(1);
				if (value == null)
					value = 0.0;
				totalOut += value;
				value = item.getStrategyCosts().get(id).get(2);
				if (value == null)
					value = 0.0;
				totalAlly += value;
			}
		}

		double todayIn = totalIn - baseIn;
		if (todayIn < 0)
			todayIn = 0.0;

		double todayOut = totalOut - baseOut;
		if (todayOut < 0)
			todayOut = 0.0;
		double todayAlly = totalAlly - baseAlly;
		if (todayAlly < 0)
			todayAlly = 0.0;
		cost.setId(id);
		cost.setTodayIn(todayIn);
		cost.setTodayOut(todayOut);
		cost.setTodayAlly(todayAlly);
		cost.setTotalAlly(totalAlly);
		cost.setTotalIn(totalIn);
		cost.setTotalOut(totalOut);
		return cost.build();
	}

	public static JsonArray getStrategyCosts(Set<MonitorReportData> baseReports,
			Set<MonitorReportData> currentReports, List<Integer> ids) {

		JsonArray array = new JsonArray();
		for (int id : ids) {
			SyncReport.Cost cost = ReportUtils.getStrategyCost(baseReports, currentReports, id);
			JsonObject obj = new JsonObject();
			obj.putNumber("id", id);
			obj.putNumber("total_in", cost.getTotalIn());
			obj.putNumber("today_in", cost.getTodayIn());
			obj.putNumber("total_out", cost.getTotalOut());
			obj.putNumber("today_out", cost.getTodayOut());
			obj.putNumber("total_ally", cost.getTotalAlly());
			obj.putNumber("today_ally", cost.getTodayAlly());
			array.add(obj);
		}
		return array;
	}
	
	public static SyncReport.StrategyImpr getStrategyImpression(Set<MonitorReportData> baseReports,
			Set<MonitorReportData> currentReports, Integer id) {
		SyncReport.StrategyImpr.Builder impression = SyncReport.StrategyImpr.newBuilder();
		long baseIn = 0, baseOut = 0, baseAlly = 0;
		long totalIn = 0, totalOut = 0, totalAlly = 0;
		for (MonitorReportData item : baseReports) {
			if (item.getStrategyImpressions().get(id) != null) {
				Long value = item.getStrategyImpressions().get(id).get(0);
				if (value == null)
					value = 0L;
				baseIn += value;
				value = item.getStrategyImpressions().get(id).get(1);
				if (value == null)
					value = 0L;
				baseOut += value;
				value = item.getStrategyImpressions().get(id).get(2);
				if (value == null)
					value = 0L;
				baseAlly += value;
			}
		}

		for (MonitorReportData item : currentReports) {
			if (item.getStrategyImpressions().get(id) != null) {
				Long value = item.getStrategyImpressions().get(id).get(0);
				if (value == null)
					value = 0L;
				totalIn += value;
				value = item.getStrategyImpressions().get(id).get(1);
				if (value == null)
					value = 0L;
				totalOut += value;
				value = item.getStrategyImpressions().get(id).get(2);
				if (value == null)
					value = 0L;
				totalOut += value;
			}
		}

		long todayIn = totalIn - baseIn;
		if (todayIn < 0)
			todayIn = 0L;

		long todayOut = totalOut - baseOut;
		if (todayOut < 0)
			todayOut = 0L;

		long todayAlly = totalAlly - baseAlly;
		if (todayAlly < 0)
			todayAlly = 0L;
		impression.setId(id);
		impression.setTodayAlly(todayAlly);
		impression.setTodayIn(todayIn);
		impression.setTodayOut(todayOut);
		impression.setTotalAlly(totalAlly);
		impression.setTotalIn(totalIn);
		impression.setTotalOut(totalOut);
		return impression.build();
	}

	public static JsonArray getStrategyImpressions(
			Set<MonitorReportData> baseReports,
			Set<MonitorReportData> currentReports, List<Integer> ids) {

		JsonArray array = new JsonArray();
		for (int id : ids) {
			SyncReport.StrategyImpr impression = ReportUtils.getStrategyImpression(baseReports, currentReports, id);
			JsonObject obj = new JsonObject();
			obj.putNumber("id", id);
			obj.putNumber("total_in", impression.getTotalIn());
			obj.putNumber("today_in", impression.getTodayIn());
			obj.putNumber("total_out", impression.getTotalOut());
			obj.putNumber("today_out", impression.getTodayOut());
			obj.putNumber("total_ally", impression.getTotalAlly());
			obj.putNumber("today_ally", impression.getTodayAlly());
			array.add(obj);
		}
		return array;
	}
	
	public static SyncReport.Click getStrategyClick(Set<MonitorReportData> baseReports,
			Set<MonitorReportData> currentReports, Integer id) {
		SyncReport.Click.Builder click = SyncReport.Click.newBuilder();
		long base = 0;
		long total = 0;
		for (MonitorReportData item : baseReports) {
			Long value = item.getStrategyClicks().get(id);
			if (value == null)
				value = 0L;
			base += value;
		}

		for (MonitorReportData item : currentReports) {
			Long value = item.getStrategyClicks().get(id);
			if (value == null)
				value = 0L;
			total += value;
		}

		long today = total - base;
		if (today < 0)
			today = 0L;
		click.setId(id);
		click.setToday(today);
		click.setTotal(total);
		return click.build();
	}

	public static JsonArray getStrategyClicks(Set<MonitorReportData> baseReports,
			Set<MonitorReportData> currentReports, List<Integer> ids) {

		JsonArray array = new JsonArray();
		for (int id : ids) {
			
			SyncReport.Click click = ReportUtils.getStrategyClick(baseReports, currentReports, id);
			JsonObject obj = new JsonObject();
			obj.putNumber("id", click.getId());
			obj.putNumber("total", click.getTotal());
			obj.putNumber("today", click.getToday());
			array.add(obj);
		}
		return array;
	}

}
